package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Hospitalization;
import com.woniuxy.entity.RechargeMoney;
import com.woniuxy.entity.Tde;
import com.woniuxy.entity.Withdrawal;
import com.woniuxy.entity.dto.CheckMoneyDTO;
import com.woniuxy.mapper.DrugPatientMapper;
import com.woniuxy.mapper.HospitalizationMapper;
import com.woniuxy.mapper.RechargeMoneyMapper;
import com.woniuxy.mapper.TdeMapper;
import com.woniuxy.service.HospitalizationService;
import com.woniuxy.service.SaasService;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class HospitalizationServiceImpl implements HospitalizationService, SaasService {
    @Autowired
    private RechargeMoneyMapper rechargeMoneyMapper;
    @Autowired
    private HospitalizationMapper hospitalizationMapper;
    @Autowired
    private TdeMapper tdeMapper;
    @Autowired
    private DrugPatientMapper drugPatientMapper;
    @Override
    public ResponseData<?> queryAll(PageParam<Hospitalization> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());

        List<Hospitalization> hospitalizationList=hospitalizationMapper.queryAll(pageParam.getParam());
        PageInfo pageInfo =new PageInfo(hospitalizationList);

        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> queryList() {
        List<Tde> tdeList = tdeMapper.queryList();
        return new ResponseData<>().ok(tdeList);
    }

    @Override
    public ResponseData<?> payAdd(Hospitalization hospitalization) throws InterruptedException {
        BigDecimal money = hospitalization.getMoney();
        Withdrawal withdrawal = drugPatientMapper.queryByPurse(hospitalization.getDwCaseId());
        SnowflakeIdGenerator sfg=new SnowflakeIdGenerator();
        long prNo=sfg.nextId();

        RechargeMoney rechargeMoney = new RechargeMoney();
        rechargeMoney.setPrMoney(money);
        rechargeMoney.setPrCaseid(hospitalization.getDwCaseId());
        rechargeMoney.setPrNo(String.valueOf(prNo));
        rechargeMoneyMapper.add(rechargeMoney);
        withdrawal.setPurse(withdrawal.getPurse().add(money));
        withdrawal.setDwCaseId(hospitalization.getDwCaseId());
        withdrawal.setRecharge(withdrawal.getRecharge().add(money));



        drugPatientMapper.update(withdrawal);



        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> queryHosAll(PageParam<Hospitalization> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
        List<Hospitalization> hospitalizationList=hospitalizationMapper.queryHosAll(pageParam.getParam());
        for (Hospitalization hospitalization : hospitalizationList) {
         BigDecimal total=hospitalization.getMoney().multiply(BigDecimal.valueOf(hospitalization.getDay())) ;
         hospitalization.setTotal(total);
         BigDecimal finalMoney=hospitalization.getPurse().subtract(total);
         hospitalization.setFinMoney(finalMoney);

        }



        PageInfo pageInfo =new PageInfo(hospitalizationList);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> queryPatient(Integer dwCaseId) {
        Hospitalization hospitalization = hospitalizationMapper.queryPatient(dwCaseId);
        Withdrawal withdrawal=new Withdrawal();
        withdrawal.setDwCaseId(dwCaseId);
        withdrawal.setPurse(BigDecimal.valueOf(0));
        drugPatientMapper.update(withdrawal);
        hospitalizationMapper.updateStatus(withdrawal);

        return new ResponseData<>().ok(hospitalization);

    }
}
